using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Impact.Infrastructure.Plugins
{

    /// <summary>
    /// a PluginParameter is an object that contains a single setting for a plugin instantiation. When a plugin
    /// is added to a page, its settings are editable independantly of other plugins of the same type.  A single
    /// plugin may have n PluginParameters.
    /// </summary>
    [Serializable]
    public class PluginParameter : ISerializable
    {
        private string name;
        private Type type;
        private string value;
        private bool isRequired;
        private IImpactPlugin plugin;

        /// <summary>
        /// default constructor
        /// </summary>
        /// <param name="plugin">the plugin that owns this parameter</param>
        /// <param name="name">the name of the parameter</param>
        /// <param name="type">the type of the parameter</param>
        /// <param name="isRequired">is this parameter required?</param>
        public PluginParameter(IImpactPlugin plugin, string name, Type type, bool isRequired)
        {
            this.plugin = plugin;
            this.name = name;
            this.type = type;
            this.isRequired = isRequired;
        }

        /// <summary>
        /// gets or sets the Name of the parameter
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// gets or sets the Type of the parameter
        /// </summary>
        public Type Type
        {
            get { return type; }
            set { type = value; }
        }

        /// <summary>
        /// gets or sets the Value of the parameter as a string
        /// </summary>
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        /// <summary>
        /// does this parameter require a value in the admin screen?
        /// </summary>
        public bool IsRequired
        {
            get { return isRequired; }
            set { isRequired = value; }
        }

        /// <summary>
        /// gets the owner plugin of this parameter
        /// </summary>
        public IImpactPlugin Plugin
        {
            get { return plugin; }
        }


        #region ISerializable Members
        /// <summary>
        /// serialization constructor
        /// </summary>
        public PluginParameter(SerializationInfo info, StreamingContext context)
        {
            this.Name = info.GetString("Name");
            this.Type = Type.GetType(info.GetString("Type"));
            this.Value = info.GetString("Value");

            Guid pluginId = (Guid)info.GetValue("PluginId", typeof(Guid));
            plugin = PluginCache.Instance.GetPlugin(pluginId);

        }

        /// <summary>
        /// serialization accessor
        /// </summary>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", Name);
            info.AddValue("Type", Type.ToString());
            info.AddValue("Value", Value);
            info.AddValue("PluginId", Plugin.PluginId, typeof(Guid));
        }

        #endregion

    }

}
